package org.javacore.concurrent;

import java.util.concurrent.CountDownLatch;

/**
 * @author liufan
 * @version 1.0
 * @date 2023/3/2 17:41
 */
public class CountDownLatchDemo {

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(5);
        for (int i = 0; i < 5; i++) {
            new Thread(new readNum(i, countDownLatch)).start();

        }
        countDownLatch.await();
        System.out.println("各个子线程执行结束");
        System.out.println("主线程执行结束");
    }

    static class readNum implements Runnable {
        private int id;
        private CountDownLatch latch;

        public readNum(int id, CountDownLatch latch) {
            this.id = id;
            this.latch = latch;
        }


        @Override
        public void run() {
            synchronized (this) {
                System.out.println("id: " + id + Thread.currentThread().getName());
                System.out.println("线程组" + id + "结束，其他线程继续");
                latch.countDown();
            }

        }


    }
}
